from approval_service.approval.mappers.approval_mapper import ApprovalMapper
from approval_service.approval.mappers.travel_approval_mapper import TravelApprovalMapper
from approval_service.approval.service.client.travel_client import TravelClient
from approval_service.approval.service.client.user_client import UserClient
from commons.entity.enum.travel_approval_enum import TravelApprovalEnum
from commons.entity.po.approval import Approval
from commons.entity.po.travel_approval import TravelApproval
from commons.entity.query.approval_query import ApprovalQuery
from commons.entity.query.travel_approval_query import TravelApprovalQuery
from commons.entity.vo.pagination_result_vo import PaginationResultVO
from commons.entity.vo.travel_vo import TravelVO
from commons.utils.copy_tools import CopyTools


class TravelApprovalService:
    def __init__(self):
        self.travel_approval_mapper = TravelApprovalMapper()
        self.travel_client = TravelClient()
        self.user_client = UserClient()
        self.approval_mapper = ApprovalMapper()

    def find_list_by_param(self, travel_approval_query=None):
        """
        业务逻辑：根据多个参数查询审批出差申请记录列表。
        :param travel_approval_query: TravelApprovalQuery 实例，包含查询过滤条件。
        :return: 包含审批出差申请记录信息字典的列表。
        """
        print("TravelApprovalService find_list_by_param")
        print(travel_approval_query)

        return self.travel_approval_mapper.select_list(TravelApproval, travel_approval_query)

    def find_count_by_param(self, travel_approval_query=None):
        """
        业务逻辑：根据多个参数统计审批出差申请记录数量。
        :param travel_approval_query: TravelApprovalQuery 实例，包含查询过滤条件。
        :return: 符合条件的审批出差申请记录数量。
        """
        print("TravelApprovalService find_count_by_param")
        print(travel_approval_query)

        return self.travel_approval_mapper.select_count(TravelApproval, travel_approval_query)

    def get_travel_approval_by_travel_approval_id(self, travel_approval_id):
        """
        业务逻辑：获取单条审批出差申请记录。
        :param travel_approval_id: str
        :return: 审批出差申请记录字典或 None
        """
        print("TravelApprovalService get_travel_approval_by_travel_approval_id ")
        print(travel_approval_id)

        return self.travel_approval_mapper.select_by_travel_approval_id(travel_approval_id)

    def add(self, travel_approval_bean):
        """
        业务逻辑：添加一个新的审批出差申请记录。
        :param travel_approval_bean: TravelApproval 类实例，包含出差申请信息。
        :return: 插入操作是否成功（True/False）。
        """
        print("TravelApprovalService add travel_approval")
        print(travel_approval_bean)

        return self.travel_approval_mapper.insert(travel_approval_bean)

    def add_batch(self, travel_approval_list_bean):
        """
        业务逻辑：批量添加审批出差申请记录。
        :param travel_approval_list_bean: 包含多个 TravelApproval 类实例的列表，包含需要插入的出差申请记录信息。
        :return: 插入操作成功的审批出差申请记录数量。
        """
        print("TravelApprovalService add_batch travel_approval")
        print(travel_approval_list_bean)

        if not travel_approval_list_bean:
            print(
                "No travel approval records to add. The input list is either null or empty.")
            return 0

        return self.travel_approval_mapper.insert_batch(travel_approval_list_bean)

    def add_or_update_batch(self, travel_approval_list_bean):
        """
        业务逻辑：批量插入或更新审批出差申请记录信息。
        :param travel_approval_list_bean: 包含多个 TravelApproval 类实例的列表，包含需要插入或更新的出差申请记录信息。
        :return: 插入或更新成功的审批出差申请记录数量。
        """
        print("TravelApprovalService add_or_update_batch travel_approval")
        print(travel_approval_list_bean)

        if not travel_approval_list_bean:
            print(
                "No travel approval records to add or update. The input list is either null or empty.")
            return 0

        return self.travel_approval_mapper.insert_or_update_batch(TravelApproval, travel_approval_list_bean)

    def update_by_param(self, travel_approval_bean, travel_approval_query):
        """
        业务逻辑：根据多个条件更新审批出差申请记录信息。
        :param travel_approval_bean: TravelApproval 类实例或字典，包含需要更新的字段及其新值。
        :param travel_approval_query: TravelApprovalQuery 类实例或字典，包含用于定位记录的过滤条件。
        :return: 更新成功的数量。
        """
        print("TravelApprovalService update_by_param")
        print(travel_approval_bean)
        print(travel_approval_query)

        return self.travel_approval_mapper.update_by_params(TravelApproval, travel_approval_bean, travel_approval_query)

    def delete_by_param(self, travel_approval_query):
        """
        业务逻辑：根据多个条件删除审批出差申请记录。
        :param travel_approval_query: TravelApprovalQuery 类实例或字典，包含用于定位记录的过滤条件。
        :return: 删除成功的数量。
        """
        print("TravelApprovalService delete_by_param")
        print(travel_approval_query)

        return self.travel_approval_mapper.delete_by_param(TravelApproval, travel_approval_query)

    def update_travel_approval_by_travel_approval_id(self, travel_approval_bean, travel_approval_id):
        """
        业务逻辑：根据 travel_approval_id 更新审批出差申请记录信息。
        :param travel_approval_id: str
        :param travel_approval_bean: TravelApproval 类实例，包含需要更新的出差申请记录属性。
        :return: 更新成功信息或错误信息。
        """
        print("TravelApprovalService update_travel_approval_by_travel_approval_id ")
        print(
            f"travel_approval_id: {travel_approval_id}, travel_approval_bean: {travel_approval_bean}")

        return self.travel_approval_mapper.update_by_travel_approval_id(travel_approval_bean, travel_approval_id)

    def delete_travel_approval_by_travel_approval_id(self, travel_approval_id):
        """
        业务逻辑：根据 travel_approval_id 删除审批出差申请记录。
        :param travel_approval_id: str
        :return: 删除成功或错误。
        """
        print("TravelApprovalService delete_travel_approval_by_travel_approval_id ")
        print(travel_approval_id)

        return self.travel_approval_mapper.delete_by_travel_approval_id(travel_approval_id)

    def get_approve_travel_record(self, user_id, page_no, page_size):
        """
        业务逻辑：获取审批的出差记录列表，包含分页信息。
        :param user_id: 经理ID
        :param page_no: 当前页码
        :param page_size: 每页显示数量
        :return: PaginationResultVO 对象，包含分页结果和审批的出差记录
        """
        print(
            f"TravelApprovalService get_approve_travel_record: user_id={user_id}, page_no={page_no}, page_size={page_size}")

        # 查询已审批的出差申请记录
        travel_approval_query = TravelApprovalQuery(manager_id=user_id)
        travel_approval_list = self.find_list_by_param(travel_approval_query)

        if not travel_approval_list:
            return None

        # 获取出差申请信息
        travel_id_list = list(
            set([travel_approval.travel_id for travel_approval in travel_approval_list]))
        travel_list = self.travel_client.get_all_travels_by_id_list(
            travel_id_list)

        # 获取用户信息
        user_id_list = list(set([travel.user_id for travel in travel_list]))
        user_list = self.user_client.get_all_users_by_id_list(user_id_list)
        user_map = {user.user_id: user for user in user_list}

        travel_vo_list = []

        for travel in travel_list:
            travel_vo = CopyTools.copy(travel, TravelVO)

            # 获取审批信息
            approval_query = ApprovalQuery(travel_id=travel.travel_id)
            approval_list = self.approval_mapper.select_list(
                Approval, approval_query)
            approval = approval_list[0]
            travel_vo.approval_id = approval.approval_id
            travel_vo.travel_status = approval.travel_approval
            travel_vo.travel_status_name = TravelApprovalEnum.get_by_type(
                approval.travel_approval).msg
            travel_vo.travel_rejection = approval.travel_rejection

            # 获取出差申请人信息
            user = user_map.get(travel.user_id)
            travel_vo.user_name = user.name
            travel_vo.user_phone = user.phone
            travel_vo.user_email = user.email
            travel_vo.position = user.position

            # 获取出差相关费用
            travel_vo.budget = (travel.food_cost +
                                travel.hotel_cost + travel.transport_cost)

            travel_vo_list.append(travel_vo)

        count = len(travel_vo_list)

        # start = (page_no - 1) * page_size
        # end = min(start + page_size, count)

        # total_page = count // page_size if count % page_size == 0 else (
        #     count // page_size) + 1

        return PaginationResultVO(count, page_size, page_no, 0, travel_vo_list)

    def get_travel_approval_by_travel_id(self, travel_id):
        """
        业务逻辑：获取单条审批出差申请记录。
        :param travel_id: str
        :return: 审批出差申请记录字典或 None
        """
        print("TravelApprovalService get_travel_approval_by_travel_id ")
        print(travel_id)

        return self.travel_approval_mapper.select_by_travel_id(travel_id)